﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using GraphDrawer.GraphProvider;

namespace GraphDrawer {
	public partial class InoculationStrategies : UserControl, IWizardController {
		public static readonly GraphProviderServiceSoapClient graphProvider = new GraphProviderServiceSoapClient();

		private Root root;
		private Source networkSource;
		private InoculationPanel inoculationPanel;

		object graph;
		Type graphType;

		public EmptyPopUp PopUp {
			get;
			set;
		}

		public InoculationStrategies(Root root) {
			InitializeComponent();

			this.root = root;
			PopUp = new EmptyPopUp();

			networkSource = new Source(this, graphProvider);
			wizard.Pages.Add(networkSource);
			inoculationPanel = new InoculationPanel(this, graphProvider);
			wizard.Pages.Add(inoculationPanel);

			DisableNext();

			PopUp.Visibility = Visibility.Collapsed;
			LayoutRoot.Children.Add(PopUp);

			networkSource.isSelectedSource += new RoutedEventHandler(networkSource_isSelectedSource);
			SizeChanged += new SizeChangedEventHandler(WizardMenu_SizeChanged);
			wizard.PageEvent += new WizardPageEvent(WizardMenu_PageEvent);
		}

		public void EnableNext() {
			wizard.btNext.IsEnabled = true;
		}

		public void DisableNext() {
			wizard.btNext.IsEnabled = false;
		}

		private void fullScreen_Click(object sender, RoutedEventArgs e) {
			//TODO: add changing style App.Current.Resources
			App.Current.Host.Content.IsFullScreen = !App.Current.Host.Content.IsFullScreen;
		}

		private void networkSource_isSelectedSource(object sender, RoutedEventArgs e) {
			if (networkSource.sourceMethod == typeof(ImportNetwork)) {
				graph = networkSource.import.graphML;
				graphType = typeof(string);
			}
			else if (networkSource.sourceMethod == typeof(GenerateNetwork)) {
				graph = networkSource.generator.graph;
				graphType = typeof(Graph);
			}
			else if (networkSource.sourceMethod == typeof(DatabaseNetworkSelector)) {
				graph = networkSource.database.graphML;
				graphType = typeof(string);
			}
			
			EnableNext();
		}

		private void WizardMenu_SizeChanged(object sender, SizeChangedEventArgs e) {
			wizard.Height = ActualHeight;
			wizard.Width = ActualWidth;

			PopUp.Width = e.NewSize.Width;
			PopUp.Height = e.NewSize.Height;
		}

		private void WizardMenu_PageEvent(Wizard sender, WizardEventArgs e) {
			if (e.Action == WizardAction.Finish || e.Action == WizardAction.Exit) {
				DisableNext();
				wizard.btNext.Content = "Next";
				wizard.ctrPageHost.SwitchPage(wizard.Pages[0]);
				root.LoadMainMenu();
			}
			else if (e.NewPageIndex == 0) { // Network source
				wizard.btNext.Content = "Next";
				DisableNext();
			}
			else if (e.NewPageIndex == 1) { // Inoculation panel
				wizard.btPrevious.IsEnabled = true;
				inoculationPanel.Init(graph, graphType);
			}
		}
	}
}
